<?php
/**
 * ResMania
 * More Information contact: info@resmania.com
 *
 * @package ResMania Installation Check Class
 * @version 0.5
 * @author webformatique
 * @copyright (C) 2010- webformatique
 * @license GNU/GPLv3 http://www.gnu.org/copyleft/gpl.html
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

class ResMania_Installer_Check {

    /**
     * Checks if the ResMania Core framework is present
     * @return boolean
     */
    function checkInstalled(){

        $core = true;
        $dbchk = true;

        if (!file_exists(JPATH_SITE. DS .'RM'.DS."system".DS."application".DS."classes".DS."RM".DS."Connector.php")) {
            $core = false;
        }

        $db =& JFactory::getDBO();
        $query = "SELECT db_version FROM rm_system WHERE id='1' LIMIT 1";
        $db->setQuery( $query );
        $version = $db->loadResult();

        if (!$version){
            $dbchk = false;
        }

        if (!$dbchk || !$core) {
            // reset the request params...
            $_REQUEST['act'] = 'install';
            $_REQUEST['act'] = 'checks';
            return false;
        }
        return true;
    }

    /**
     * Handles the ResMania core installation.
     * This is just a helper that helps the user install the core framework
     * Basically a download for this library and a db update.
     */

    function InstallSwitch($overrideVersion){

        if (isset($_REQUEST['act']) && $_REQUEST['act'] == 'install' && isset($_REQUEST['task'])){

            $rminstall = new ResMania_Install_Controller($overrideVersion);

            if (!isset($_REQUEST['sid'])) {
                $model = new ResMania_Install_Model();
                $sid = $model->getSID();
            } else {
                $sid = $_REQUEST['sid'];
            }

            // installation AJAX process switcher...
            switch ($_REQUEST['task']){
                case "getversions":
                    $rminstall->getVersions($overrideVersion);
                    break;
                case "checks":
                    $rminstall->checks(JPATH_SITE,$sid);
                    break;
                case "download":
                    $licensekey = $_REQUEST['licensekey'];
                    if (isset($_REQUEST['resume'])){$noresume = $_REQUEST['resume'];}
                    $rminstall->download($sid, $overrideVersion, $licensekey, $noresume);
                    break;
                case "downloadprogress":
                    $rminstall->downloadProgress($sid);
                    break;
                case "unzipcore":
                    $rminstall->unzipCore();
                    break;
                case "setupDB":
                    $rminstall->setupDB();
                    break;
                case 'postcheck':
                    $rminstall->postCheck();
                    break;
            }

        }
    }
}
?>
